using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace XWord.WPF.ApplicationFramework.Extensions
{
    public static class VisualExtension
    {
        public static T FindAncestor<T>(this DependencyObject dependencyObject) where T : class
        {
            while (dependencyObject != null && !(dependencyObject is T))
            {
                dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
            }

            return dependencyObject as T;
        }

        public static T FindAncestor<T>(this Visual visual) where T : Visual
        {
            while (visual != null && !(visual is T))
            {
                visual = (Visual)VisualTreeHelper.GetParent(visual);
            }
            return (T)visual;
        }

        public static int FindNullAncestorCount(this DependencyObject visual)
        {
            int count = 0;
            while (visual != null)
            {
                visual = VisualTreeHelper.GetParent(visual);
                count++;
            }
            return count;
        }

        public static T FindAncestor<T>(this Visual visual, Predicate<T> predicate) where T : Visual
        {
            while (visual != null && !predicate(visual as T))
            {
                visual = (Visual)VisualTreeHelper.GetParent(visual);
            }
            return (T)visual;
        }

        public static T FindAncestorByLogicalTree<T>(this Visual visual) where T : Visual
        {
            while (visual != null && !(visual is T))
            {
                visual = (Visual)LogicalTreeHelper.GetParent(visual);
            }
            return (T)visual;
        }

        public static T FindVisualChild<T>(this DependencyObject obj) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is T)
                    return (T)child;
                else
                {
                    T childOfChild = child.FindVisualChild<T>();
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

        public static T FindVisualChild<T>(this DependencyObject obj, string uid) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                var child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is T)
                {
                    var uiElement = child as UIElement;
                    if (uiElement != null && uiElement.Uid == uid)
                        return (T)child;
                }
                else
                {
                    T childOfChild = child.FindVisualChild<T>(uid);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

        public static void LoopVisualChild(this DependencyObject obj, Action<DependencyObject> action)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null)
                {
                    action(child);
                    child.LoopVisualChild(action);
                }
            }
        }

        public static FrameworkElement FindVisualChild(this DependencyObject obj, Type type)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                FrameworkElement child = VisualTreeHelper.GetChild(obj, i) as FrameworkElement;

                if (child != null && child.GetType().Equals(type))
                    return child;
                else
                {
                    FrameworkElement childOfChild = FindVisualChild(child, type);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

        public static bool InVisualTree(this FrameworkElement obj)
        {
            return VisualTreeHelper.GetParent(obj)!=null && VisualTreeHelper.GetChildrenCount(obj) > 0;
        }

        public static FrameworkElement FindVisualChild(this FrameworkElement obj, string name)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                FrameworkElement child = VisualTreeHelper.GetChild(obj, i) as FrameworkElement;

                if (child != null && child.Name == name)
                    return child;
                else
                {
                    FrameworkElement childOfChild = FindVisualChild(child, name);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }
    }
}